
#include <BFont>
#include <BSizePolicy>

#include "member_BTableItem.h"

using namespace BWE;

member_BTableItem::member_BTableItem(BTableItem* tableWidgetItem)
{
	boss = tableWidgetItem;
	table = 0;
	align = Align_Left | Align_VCenter;
	widget = 0;
	row = -1;
	column = -1;
	editable = false;
	lineFeed = false;
}
member_BTableItem::~member_BTableItem()
{

}

BSize member_BTableItem::sizeHint(const BFont* font)
{
	if (widget && widget->visible())
	{
		return widget->sizeHint();
	}
	if (font)
	{
		textSize = font->textSize(text);
		return textSize;
	}
	return BSize();
}

void member_BTableItem::dirtyCell()
{
	dirty = true;
	if (table)
	{
		table_member(table)->dirty = true;
		table->fresh();
	}
}
void member_BTableItem::adjust(int x, int y, int w, int h)
{
	BRect temp(x, y, w, h);
	if (rect == temp)
		return;
	rect = temp;
	if (!table)
		return;
	BRect imageArea(x, y, 0, h);
	if (image)
	{
		BSize imageSize = image->size();
		imageSize.wrap(textSize.height());
		imageArea.set(x, y, textSize.height(), h);
		imageRect = imageArea.align(Align_Center, imageSize);
		int size = bMax(imageRect.width(), imageRect.height());
		if (size > h)
		{
			imageRect *= (float)h / size;
		}
	}
	BRect textArea = rect;
	textArea.left() = imageArea.right();
	if (lineFeed)
		textRect = textArea;
	else
		textRect = textArea.align(align, bMin(textArea.width(), textSize.width()), textSize.height());
	if (widget)
	{
		widget->setRect(rect);
		widget->setVisible(rect.width() > 0 && rect.height() > 0);
	}
}

bool member_BTableItem::empty() const
{
	return !widget && text.empty();
}
bool member_BTableItem::ignored() const
{
	if (widget)
		return !widget->visible();
	return text.empty();
}

bool member_BTableItem::operator == (const BString& text) const
{
	return this->text == text;
}
bool member_BTableItem::operator == (const BWidget* widget) const
{
	return this->widget == widget;
}

bool member_BTableItem::operator != (const BString& text) const
{
	return this->text != text;
}
bool member_BTableItem::operator != (const BWidget* widget) const
{
	return this->widget != widget;
}
